package com.javacoo.event.client.support.ast;

import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

import com.javacoo.event.client.starter.EventHolder;
import com.javacoo.event.client.support.ApplicationContextProvider;
import com.javacoo.event.client.support.BaseEventRegister;

import lombok.extern.slf4j.Slf4j;

/**
 * AST实现事件注册
 * <p>说明:AST</p>
 * <li></li>
 *
 * @author duanyong@jccfc.com
 * @date 2021/10/16 7:50
 */
@Slf4j
@Component
public class AstEventRegister extends BaseEventRegister implements ApplicationListener<ContextRefreshedEvent> {

    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
        if (event.getApplicationContext().getParent() == null) {
            //设置applicationContext
            ApplicationContextProvider.setApplicationContext(event.getApplicationContext());
            //注册事件监听对象
            EventHolder.getEventContext().getListeningObjects().forEach(o -> EventHolder.getEventHelper().get().register(o));
        }
    }
}
